package com.jianjun.ktui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import cn.hutool.core.util.StrUtil
import cn.hutool.http.GlobalInterceptor
import cn.hutool.http.HttpRequest
import cn.hutool.http.HttpResponse
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // 设置日志目录
        val logDir = getExternalFilesDir(null)?.absolutePath + File.separator + "logs"
        File(logDir).mkdirs() // 确保目录存在
        System.setProperty("logDir", logDir)
        val log: Logger = LoggerFactory.getLogger(MainActivity::class.java)
        enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        GlobalInterceptor.INSTANCE.addRequestInterceptor { httpObj: HttpRequest ->
            log.info(
                StrUtil.sub(
                    httpObj.toString(),
                    0,
                    2000
                )
            )
        }
        GlobalInterceptor.INSTANCE.addResponseInterceptor { httpObj: HttpResponse ->
            log.info(
                StrUtil.sub(
                    httpObj.toString(),
                    0,
                    2000
                )
            )
        }
        setContent {
            App()
        }
    }
}

@Preview
@Composable
fun AppAndroidPreview() {
    App()
}